#include "CSurface.h"
#include <iostream>
using namespace std;

CSurface::CSurface()
{}

SDL_Surface* CSurface::OnLoad(char* File)
{
	SDL_Surface* Surf_Temp = NULL;
	SDL_Surface* Surf_Return = NULL;
	
	// load the image into a temporary surface
	if ((Surf_Temp = SDL_LoadBMP(File)) == NULL)
	{
		cout << File << endl;
		cout << SDL_LoadBMP(File) << endl;
		return NULL;
	}
	else
		cout << SDL_GetError() << endl;

	/* since different images may have different pixel formats, there may be some loss of colors
	or performance when SDL tries to manage the image....That's where SDL_DisplayFormat()
	comes to the rescue!

	FROM THE SDL API:
		This function takes a surface and copies it to a new surface of the pixel format
		and colors of the video framebuffer, suitable for fast blitting onto the display surface.
	*/
	Surf_Return = SDL_DisplayFormat(Surf_Temp);

	// Delete the surface when we're finish using it
	SDL_FreeSurface(Surf_Temp);

	return Surf_Return;
}

bool CSurface::OnDraw(SDL_Surface *Surf_Dest, SDL_Surface *Surf_Src, int X, int Y)
{
	// if the source surface or destination surface is null...
	if ((Surf_Dest == NULL) || (Surf_Src == NULL))
		return false;

	// SDL structure used for creating rectangles
	SDL_Rect DestR;
	DestR.x = X;
	DestR.y = Y;

	/* blit surface allows us to draw one surface...on top of the other surface...thus reducing the flickering
	between switching surfaces.

	FROM THE API:
		This performs a fast blit from the source surface to the destination surface.
	*/
	SDL_BlitSurface(Surf_Src, NULL, Surf_Dest, &DestR);
	return true;
}

bool CSurface::OnDraw(SDL_Surface* Surf_Dest, SDL_Surface* Surf_Src, int X, int Y, int X2, int Y2, int W, int H)
{
	
	// if the source surface or destination surface is null...
    if(Surf_Dest == NULL || Surf_Src == NULL)
        return false;

	
	

	// SDL structure used for creating rectangles
	SDL_Rect DestR;
	DestR.x = X;
	DestR.y = Y;

	// This time, we want to specify where exactly we want to begin copying...from the source image
	SDL_Rect SrcR;
	SrcR.x = X2;
	SrcR.y = Y2;
	SrcR.w = W;		// we set the width
	SrcR.h = H;		// and height of the image section we want to copy from.

	/* blit surface allows us to draw one surface...on top of the other surface...thus reducing the flickering
	between switching surfaces.

	FROM THE API:
		This performs a fast blit from the source surface to the destination surface.
	*/
	SDL_BlitSurface(Surf_Src, &SrcR, Surf_Dest, &DestR);	// Note how we specified the position of the source rectangle here....

	return true;
}